﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using SpLocalizer.Common;
using SpLocalizer.Common.DbApi;
using SpLocalizer.ContentSourceProviders.XPath;
using Microsoft.Practices.Unity;
using SpLocalizer.ContentSourceProviders.Eml;

namespace SpLocalizer.ContentSourceProviders
{
    public class ValoremFileTranslationProject : AbstractTranslationProject
    {
        private const string Const_XmlExtension = ".xml";
        private const string Const_AspxExtension = ".aspx";
        private const string Const_WebpartsExtension = ".webparts";
        private const string Const_EmlExtension = ".eml";

        protected override void Bind(bool IncludeDescendants)
        {
            // Open Directory or file provided as connection string property
            FileAttributes fa = File.GetAttributes(this.ConnectionString);
           

            /// Build file list for translation this might be all files from directory or single xml
            List<FileInfo> SourceFilesList = new List<FileInfo>();
            if ((fa & FileAttributes.Directory) == FileAttributes.Directory)
            {
                SourceFilesList = LoadFilesForTranslation(this.ConnectionString, IncludeDescendants);               
            }
            else
            {   // This is a File                
                SourceFilesList.Add(new FileInfo(this.ConnectionString));
            }

            foreach (FileInfo file in SourceFilesList)
                if (file.Extension.Equals(Const_XmlExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (XPathContent provider in ServiceContainer.Container.ResolveAll<XPathContent>())
                    {
                        try
                        {
                            provider.Bind(file.FullName);
                            this._ContentData.Add(provider);
                        }
                        catch (XmlException ex)
                        {
                            // todo: log exception
                        }
                    }
                }
                else if (file.Extension.Equals(Const_AspxExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (XPathContent provider in ServiceContainer.Container.ResolveAll<PublishingPageContent>())
                    {
                        try
                        {
                            provider.Bind(file.FullName);
                            this._ContentData.Add(provider);
                        }
                        catch (XmlException ex)
                        {
                            // todo: log exception
                        }
                    }
                }
                else if (file.Extension.Equals(Const_WebpartsExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (XPathContent provider in ServiceContainer.Container.ResolveAll<WebPartFileContent>())
                    {
                        provider.Bind(file.FullName);
                        this._ContentData.Add(provider);
                    }
                }
                else if (file.Extension.Equals(Const_EmlExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    EmlFileContent provider =  new EmlFileContent();
                 
                    provider.Bind(file.FullName);
                    this._ContentData.Add(provider);
                  
                }
   
            

            // Set project title
            if (string.IsNullOrEmpty(this.ProjectTitle))
            {
                this.ProjectTitle = this.ConnectionString;
            }
        }

        /// <summary>
        /// Load files as content source for translation
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static List<FileInfo> LoadFilesForTranslation(string path, bool IncludeDescendants)
        {
            // This is a Directory
            DirectoryInfo di = new DirectoryInfo(path);
            List<FileInfo> SourceFilesList = new List<FileInfo>();
            SourceFilesList.AddRange(di.GetFiles("*" + Const_XmlExtension));
            SourceFilesList.AddRange(di.GetFiles("*" + Const_AspxExtension));
            SourceFilesList.AddRange(di.GetFiles("*" + Const_WebpartsExtension));
            SourceFilesList.AddRange(di.GetFiles("*" + Const_EmlExtension));
            // Load subdirictories if we IncludeDescendants
            if (IncludeDescendants){
                foreach (DirectoryInfo subdir in di.GetDirectories())
                    SourceFilesList.AddRange(LoadFilesForTranslation(subdir.FullName, IncludeDescendants));
            }
            return SourceFilesList;
        }

        protected override AbstractContent LoadContent(LocalizableContent content)
        {
            var xPathContent = ServiceContainer.Resolve<XPathContent>(content.ContentSourceProvider);
            xPathContent.Bind(content.ContentAddress);
            return xPathContent;
        }

        protected override void RegisterContentProviderTypes()     {
           // Do Nothing
            ServiceContainer.Container.RegisterType<XPathContent, XPathContent>(typeof(XPathContent).Name);
            ServiceContainer.Container.RegisterType<XPathContent, PublishingPageContent>(typeof(PublishingPageContent).Name);
            ServiceContainer.Container.RegisterType<XPathContent, WebPartFileContent>(typeof(WebPartFileContent).Name);
            ServiceContainer.Container.RegisterType<XPathContent, EmlFileContent>(typeof(EmlFileContent).Name);  
            ServiceContainer.Container.RegisterType<BaseTranslationTask, XPathContentLocalizationTask>
                    (typeof(XPathContentLocalizationTask).Name);
            ServiceContainer.Container.RegisterType<BaseTranslationTask, EmlContentLocalizationTask>
                    (typeof(EmlContentLocalizationTask).Name);
        }
       
    }
}
